﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Assesment.Library;
using Assesment.Library.DomainObject.Administration.Catalog;
using Assesment.Library.Enum;
using Assesment.Library.Interfaces;
using SIPO.Library.DomainObject.Administration.Catalog;
using Assesment.Library.DomainObject.Transaction;
using Training.Library.DomainObject.Administration.Catalog;

namespace Assesment.Library.DataAccess.Transaction
{
   public partial class AssesmentPositionIDPDetailRepo
    {
       public List<AssesmentPositionIDPDetail> GetListByAssesmentIdpID_CompetencyDesc(int AssesmentIdpID, string CompetencyDesc)
       {
           List<AssesmentPositionIDPDetail> tt = new List<AssesmentPositionIDPDetail>();
           using (SqlConnection conn = new SqlConnection(connectionString))
           {
               using (SqlCommand cmd = conn.CreateCommand())
               {

                   cmd.CommandText = "SP_AssesmentPositionIDPDetail_AssesmentIdpID_CompetencyDesc";
                   cmd.CommandType = CommandType.StoredProcedure;
                   cmd.Parameters.Add("@AssesmentIdpID", SqlDbType.Int).Value = AssesmentIdpID;
                   cmd.Parameters.Add("@CompetencyDesc", SqlDbType.VarChar).Value = CompetencyDesc;
                   try
                   {
                       conn.Open();
                       using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                       {
                           while (dr.Read())
                           {
                               AssesmentPositionIDPDetail t = new AssesmentPositionIDPDetail(true);
                               t.AssesmentIdpDtlID = dr[AssesmentPositionIDPDetail.Fields.AssesmentIdpDtlID] == DBNull.Value ? 0 : int.Parse(dr[AssesmentPositionIDPDetail.Fields.AssesmentIdpDtlID].ToString());
                               t.AssesmentIdpID = dr[AssesmentPositionIDPDetail.Fields.AssesmentIdpID] == DBNull.Value ? (int?)null : int.Parse(dr[AssesmentPositionIDPDetail.Fields.AssesmentIdpID].ToString());
                               t.AssesmentPositionDtlID = dr[AssesmentPositionIDPDetail.Fields.AssesmentPositionDtlID] == DBNull.Value ? (int?)null : int.Parse(dr[AssesmentPositionIDPDetail.Fields.AssesmentPositionDtlID].ToString());
                               t.CompetencyID = dr[AssesmentPositionIDPDetail.Fields.CompetencyID] == DBNull.Value ? (int?)null : int.Parse(dr[AssesmentPositionIDPDetail.Fields.CompetencyID].ToString());
                               t.DevelopmentActivity = dr[AssesmentPositionIDPDetail.Fields.DevelopmentActivity] == DBNull.Value ? string.Empty : dr[AssesmentPositionIDPDetail.Fields.DevelopmentActivity].ToString();
                               t.MethodID = dr[AssesmentPositionIDPDetail.Fields.MethodID] == DBNull.Value ? (int?)null : int.Parse(dr[AssesmentPositionIDPDetail.Fields.MethodID].ToString());
                               t.SupportingPeople = dr[AssesmentPositionIDPDetail.Fields.SupportingPeople] == DBNull.Value ? string.Empty : dr[AssesmentPositionIDPDetail.Fields.SupportingPeople].ToString();
                               t.StartDate = dr[AssesmentPositionIDPDetail.Fields.StartDate] == DBNull.Value ? string.Empty : dr[AssesmentPositionIDPDetail.Fields.StartDate].ToString();
                               t.EndDate = dr[AssesmentPositionIDPDetail.Fields.EndDate] == DBNull.Value ? string.Empty : dr[AssesmentPositionIDPDetail.Fields.EndDate].ToString();
                               t.CreatedDate = dr[AssesmentPositionIDPDetail.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[AssesmentPositionIDPDetail.Fields.CreatedDate].ToString());
                               t.CreatedBy = dr[AssesmentPositionIDPDetail.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[AssesmentPositionIDPDetail.Fields.CreatedBy].ToString();
                               t.UpdatedDate = dr[AssesmentPositionIDPDetail.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[AssesmentPositionIDPDetail.Fields.UpdatedDate].ToString());
                               t.UpdatedBy = dr[AssesmentPositionIDPDetail.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[AssesmentPositionIDPDetail.Fields.UpdatedBy].ToString();

                               t.AssesmentPositionDetail = new AssesmentPositionDetail(true);
                               t.AssesmentPositionDetail.KeyBehaviourDetail = new KeyBehaviourDetail(true);
                               t.AssesmentPositionDetail.KeyBehaviourDetail.KeyBehaviourName = dr[KeyBehaviourDetail.Fields.KeyBehaviourName] == DBNull.Value ? string.Empty : dr[KeyBehaviourDetail.Fields.KeyBehaviourName].ToString();
                               
                               t.AssesmentPositionDetail.KeyBehaviourDetail.LevelCompetency.Level = dr[LevelCompetency.Fields.Level] == DBNull.Value ? (short?)null : short.Parse(dr[LevelCompetency.Fields.Level].ToString());

                               tt.Add(t);
                               t.Dispose();
                           }
                       }
                   }
                   catch (Exception ex)
                   {
                       throw (ex);
                   }
                   finally
                   {
                       if (conn.State == ConnectionState.Open) conn.Close();
                   }
               }
           }
           return tt;
       }		
    }
}
